﻿namespace NetFx.SMIL.Resolve
{
  using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Text;
  using System.Reflection;
  using System.Reflection.Emit;

  using NetFx.SMIL.Utils;


  [System.Serializable]
  public class UnableToResolveException : System.Exception
  {
    public UnableToResolveException() { }
    public UnableToResolveException(string message) : base(message) { }
    public UnableToResolveException(string message, System.Exception inner) : base(message, inner) { }
    protected UnableToResolveException(
      System.Runtime.Serialization.SerializationInfo info,
      System.Runtime.Serialization.StreamingContext context)
      : base(info, context) { }
  }

  /// <summary>
  /// 成员的引用。
  /// </summary>
  public class MemberReference
  {
    #region Fields
    /// <summary>
    /// 成员的全名。
    /// </summary>
    /// <remarks>
    /// 全名用于以文字形式定位成员，
    /// 例如类，字段或者方法（名）。
    /// </remarks>
    protected FullName fullname = null;
    /// <summary>
    /// 成员信息。
    /// </summary>
    /// <remarks>
    /// 由于MemberInfo是Type的派生类，所以
    /// 无论Type是否是InnerType或者NestedType，
    /// 它都可以由MemberInfo标识，只是当它
    /// 不是InnerType的时候，它的DeclaringType
    /// 为null。
    /// </remarks>
    protected MemberInfo memberInfo = null;
    /// <summary>
    /// 成员所在的模块。
    /// </summary>
    protected Module module = null;
    #endregion
    #region Properties
    public virtual Module Module
    {
      get
      {
        return this.module;
      }
    }
    public virtual FullName FullName
    {
      get
      {
        return this.fullname;
      }
      set
      {
        if ((this.fullname == null && value != null)
          || (this.fullname != null && !this.fullname.Equals(value)))
        {
          this.UpdateMemberInfo(this.fullname = value);
        }
      }
    }
    public virtual string FullNameText
    {
      get
      {
        return this.FullName.ToString();
      }
    }
    public virtual MemberInfo MemberInfo
    {
      get
      {
        return this.memberInfo;
      }
      set
      {
        if ((this.memberInfo == null && value != null) || (this.memberInfo != null && this.memberInfo !=value))
        {
          this.UpdateFullName(this.memberInfo = value);
        }
      }
    }
    #endregion
    #region Constructors
    public MemberReference(Module module, MemberInfo memberInfo)
    {
      if (module == null) throw new ArgumentNullException("module can not be null!");
      this.module = module;
      this.MemberInfo = memberInfo;
    }
    public MemberReference(Module module, FullName fullname)
    {
      if (module == null) throw new ArgumentNullException("module can not be null!");
      this.module = module;
      this.FullName = fullname;
    }
    #endregion
    #region Methods

    protected virtual void UpdateMemberInfo(FullName fullname)
    {
      if (fullname != null)
      {

        //NetFx.SMIL.Resolve.FullName f = new FullName("", "", "");
      }
      else
      {
        this.memberInfo = null;
      }
    }

    protected virtual void UpdateFullName(MemberInfo info)
    {
      if (info != null)
      {
        //List<string> scopes = new List<string>();
        //scopes.Add(info.Name);
        //this.GetScopeList(scopes, info, info.DeclaringType);

        //this.fullname = Identifier.CreateIdentifierWith(scopes.ToArray());
        //scopes.Clear();
      }
      else
      {
        //对空信息置为空标全名。
        this.fullname = FullName.EmptyName;
      }
    }

    //protected virtual void GetScopeList(List<string> scopes,MemberInfo info,Type declaringType)
    //{
    //  if (declaringType != null)
    //  {
    //    scopes.Add(declaringType.Name);
    //    GetScopeList(scopes, declaringType.DeclaringType);
    //  }
    //  else if (info is Type)
    //  {
    //    //分解Namespace，形成对应范围列表。
    //    scopes.AddRange(
    //      Identifier.CreateIdentifierOf((info as Type).Namespace).Scopes
    //      );
    //  }
    //}
    #endregion
  }
}
